//
//  UITabBarController+FloatingTabbarExtension.m
//  F FloatingPlayer
//
//  Created by yonas on 2018/9/27.
//  Copyright © 2018年 Inspur. All rights reserved.
//

#import "UITabBarController+FloatingTabbarExtension.h"
#import <objc/runtime.h>
#import "TransitionAnimation.h"
@implementation UITabBarController (FloatingTabbarExtension)

+ (void)initialize{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSel = @selector(viewDidLoad);
        SEL swizzlingSel = @selector(floating_viewDidload);
        Method originalMethod = class_getInstanceMethod(class, originalSel);
        Method swizzlingMethod = class_getInstanceMethod(class, swizzlingSel);
        BOOL success = class_addMethod(class, swizzlingSel, method_getImplementation(swizzlingMethod), method_getTypeEncoding(swizzlingMethod));
        if (success){
            class_replaceMethod(class, swizzlingSel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        }else{
            method_exchangeImplementations(originalMethod, swizzlingMethod);
        }
    });
}

- (void)floating_viewDidload{
    [self floating_viewDidload];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showFloatingView:) name:SHOW_FLOATING_VIEW object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideFloatingView) name:HIDE_FLOATING_VIEW object:nil];
}

- (UIView *)getFloatingWindow{
    return (UIView *)[[self getFloatingController] getFloatingWindow];
}

- (CGRect)getOriginalFrame{
    return [self getFloatingModel].originalFrame;
}

- (CGRect)getViewFrame{
    return [self getFloatingModel].viewFrame;
}
- (void)hideFloatingView{
    [[self getFloatingController] floatingController_hide];
    [self setFloatingPlayerController:nil];
    [self setFloatingModel:nil];
    [self _getCurrentNav].delegate = nil;
}
#pragma mark - private


- (void)showFloatingView:(NSNotification *)notification{
    FloatingViewModel *model =  notification.object;
    FloatingViewController *floatingPlayerController = [[FloatingViewController alloc]initWithPlayerModel:model delegate:self];
    [floatingPlayerController floatingController_show];
    [self setFloatingPlayerController:floatingPlayerController];
    [self setFloatingModel:model];
    [self _getCurrentNav].delegate = self;
    //pop出去。
    [[self _getCurrentNav] popViewControllerAnimated:YES];
}
- (UINavigationController *)_getCurrentNav{
    return (UINavigationController *)self.viewControllers[self.selectedIndex];
}

#pragma mark - property

- (void)setFloatingPlayerController:(FloatingViewController *)controller{
    objc_setAssociatedObject(self, @selector(getFloatingController), controller, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (FloatingViewController *)getFloatingController{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setFloatingModel:(FloatingViewModel *)viewModel{
    objc_setAssociatedObject(self, @selector(getFloatingModel), viewModel, OBJC_ASSOCIATION_ASSIGN);
}
- (FloatingViewModel *)getFloatingModel{
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - FloatingPlayerControllerDelegate
- (void)floatingController_backToFromVcWithClass:(Class)cls floatingModel:(FloatingViewModel *)floatingModel floatingViewController:(FloatingViewController *)controller{
    
    [self _getCurrentNav].delegate = self;
    UIViewController *vc = [self floating_getFromViewControllerWithClass:cls];
    [[self _getCurrentNav] pushViewController:vc animated:YES];
}

#pragma mark - UINavigationControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPush){
        return [TransitionAnimation transitionWithType:TransitionTypePush];
    }else{
        return [TransitionAnimation transitionWithType:TransitionTypePop];
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([viewController.class isEqual:NSClassFromString([self getFloatingModel].fromVc)]){
        //避免闪烁。
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self hideFloatingView];
        });
    }
}
@end
